package com.settlement.system.model.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.settlement.system.common.base.BaseEntity;
import com.settlement.system.common.enums.OMSOrderTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
* <p>
* 标准订单明细
* </p>
*
* @author huangrensen
* @since 2024-04-23
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysOrderDetail extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
    * ID
    */
    @TableId(value = "id")
    private String id;


    /**
    * Y3单号
    */
    private String yzNo;

    /**
    * OMS单号
    */
    private String omsNo;

    /**
    * 发退货时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime opTime;


    /**
    * 下单时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime buyTime;


    /**
    * 原始主订单号
    */
    private String sourceMainNo;
    @TableField(exist = false)
    private List<String> mainNoList;


    /**
    * 原始子订单号
    */
    private String sourceSubNo;


    /**
    * 店铺ID
    */
    private Long storeId;
    @TableField(exist = false)
    private String storeName;


    /**
    * PO单号
    */
    private String poNo;


    /**
    * 商品单价
    */
    private BigDecimal price = BigDecimal.ZERO;


    /**
    * 发货数量
    */
    private Integer qty = 0;


    /**
    * 发货金额
    */
    private BigDecimal sendAmount = BigDecimal.ZERO;


    /**
    * 退货数量
    */
    private Integer returnQty = 0;


    /**
    * 退货金额
    */
    private BigDecimal returnAmount = BigDecimal.ZERO;


    /**
    * 品牌
    */
    private String brand;


    /**
    * SKU
    */
    private String sku;


    /**
    * 商品名称
    */
    private String productName;


    /**
    * 订单状态
    */
    private String orderStatus;

    /**
     * 订单类型
     */
    private Integer orderType = OMSOrderTypeEnum.ORDER.getValue();
    @TableField(exist = false)
    private String orderTypeName;

    /**
     * 关联ID
     */
    private String linkId;
    // 关联单号
    private String linkNo;

    @TableField(exist = false)
    private String tableName;

    @TableField(exist = false)
    private String billMonth;

    @TableField(exist = false)
    private LocalDateTime businessDate;

    /**
     * 核销单ID
     */
    private String auditId;
    @TableField(exist = false)
    private List<String> auditIdList;
    /**
     * 归档核销单ID
     */
    private String fileAuditId;

    public boolean isAllZero(){
        // 商品单价=0，发货数量=0，发货金额=0，退货数量=0，退货金额=0
        return compareBigDecimal(price, BigDecimal.ZERO) && Objects.equals(qty, 0) && compareBigDecimal(sendAmount, BigDecimal.ZERO) && Objects.equals(returnQty, 0) && compareBigDecimal(returnAmount, BigDecimal.ZERO);
    }

    public static boolean isDate(String field){
        return "opTime,buyTime,businessDate".contains(field);
    }
    public static boolean isDecimal(String field){
        return "returnAmount,sendAmount,price".contains(field);
    }

    public static boolean isInteger(String field){
        return "qty,returnQty".contains(field);
    }

    private static boolean compareDate(LocalDateTime date1, LocalDateTime date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 != null && date1.equals(date2)) {
            return true;
        }
        return false;
    }

    private static boolean compareBigDecimal(BigDecimal value1, BigDecimal value2) {
        if (value1 == null && value2 == null) {
            return true;
        }
        if (Objects.isNull(value2)) {
            value2 = BigDecimal.ZERO;
        }
        if (value1 != null && value1.compareTo(value2) == 0) {
            return true;
        }
        return false;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        SysOrderDetail that = (SysOrderDetail) o;
        return Objects.equals(omsNo, that.omsNo) && compareDate(opTime, that.opTime) && compareDate(buyTime, that.buyTime) && Objects.equals(sourceMainNo, that.sourceMainNo) && Objects.equals(sourceSubNo, that.sourceSubNo) && Objects.equals(storeId, that.storeId) && Objects.equals(poNo, that.poNo) && compareBigDecimal(price, that.price) && Objects.equals(qty, that.qty) && compareBigDecimal(sendAmount, that.sendAmount) && Objects.equals(returnQty, that.returnQty) && compareBigDecimal(returnAmount, that.returnAmount) && Objects.equals(brand, that.brand) && Objects.equals(sku, that.sku) && Objects.equals(productName, that.productName) && Objects.equals(orderStatus, that.orderStatus) && Objects.equals(orderType, that.orderType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), omsNo, opTime, buyTime, sourceMainNo, sourceSubNo, storeId, poNo, price, qty, sendAmount, returnQty, returnAmount, brand, sku, productName, orderStatus, orderType);
    }
}
